package cc.rengu.igas.channel.upqc.core.service.trans;


import cc.rengu.igas.channel.upqc.common.constant.UpqcParamConstant;
import cc.rengu.igas.channel.upqc.common.constant.UpqcTreeNodeConstant;
import cc.rengu.igas.channel.upqc.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upqc.common.util.UpqcSecurityUtil;
import cc.rengu.igas.channel.upqc.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upqc.core.service.base.PayService;
import cc.rengu.igas.channel.upqc.facade.bean.ComInfo;
import cc.rengu.igas.channel.upqc.facade.request.B2CConsumeCancelRequest;
import cc.rengu.igas.channel.upqc.facade.response.B2CConsumeCancelResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import org.apache.commons.codec.binary.Base64;

import java.util.Map;


/**
 * 主扫支付撤销
 */
public class B2CConsumeCancelService extends PayService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        B2CConsumeCancelRequest b2CConsumeCancelRequest = new B2CConsumeCancelRequest();
        ConvertUtil.convertOutput(b2CConsumeCancelRequest);
        return b2CConsumeCancelRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpqcTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        B2CConsumeCancelResponse b2CConsumeCancelResponse = new B2CConsumeCancelResponse();
        B2CConsumeCancelRequest b2CConsumeCancelRequest = (B2CConsumeCancelRequest) request;
        /* 根据微信支付产品类型对接口接口字段赋值 */
        ConvertUtil.convertInput(b2CConsumeCancelRequest);
        /* 根据通道标识调用不同通道进行支付 */
        xmlTreeUtil.setXmlTreeStringValue(UpqcTreeNodeConstant.txnType, b2CConsumeCancelRequest.getHeader().getTxnNum());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, b2CConsumeCancelRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        /* 恢复原交易码 */
        if (98 == iReturnCode) {
            rglog.error("调用银联二维码服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            b2CConsumeCancelResponse.setRespCode("04");
            b2CConsumeCancelResponse.setRespDesc("交易状态未明");
        } else if (0 != iReturnCode) {
            rglog.error("调用银联二维码服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(b2CConsumeCancelResponse);


        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        if (UpqcParamConstant.SUCCESS_CODE.equals(b2CConsumeCancelResponse.getRespCode())) {
            b2CConsumeCancelResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            b2CConsumeCancelResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

            if (b2CConsumeCancelResponse.getSettleDate() != null) {
                b2CConsumeCancelResponse.setSettleDate(UpqcSecurityUtil.getCupsSettleDate(b2CConsumeCancelResponse.getSettleDate()));
            }

            String comInfoStr = xmlTreeUtil.getXmlTreeStringValue(UpqcTreeNodeConstant.comInfoStr);
            if (comInfoStr != null) {
                comInfoStr = new String(Base64.decodeBase64(comInfoStr));
                rglog.error("解密的comInfo:<{}>", comInfoStr);
                ComInfo comInfo = new ComInfo();
                Map<String, Object> kvMap = UpqcSecurityUtil.parseKvString(comInfoStr.substring(1, comInfoStr.length() - 1));
                if (kvMap.get("F0") != null) {
                    comInfo.setMsgType((String) kvMap.get("F0"));
                }
                if (kvMap.get("F3") != null) {
                    comInfo.setProcCode((String) kvMap.get("F3"));
                }
                if (kvMap.get("F25") != null) {
                    comInfo.setEntryMode((String) kvMap.get("F25"));
                }
                if (kvMap.get("F37") != null) {
                    comInfo.setReferenceNo((String) kvMap.get("F37"));
                }
                if (kvMap.get("F60") != null) {
                    comInfo.setReserve((String) kvMap.get("F60"));
                }
                b2CConsumeCancelResponse.setComInfo(comInfo);
            }
        }
        chnlBizResponse.setResult(b2CConsumeCancelResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        B2CConsumeCancelResponse b2CConsumeCancelResponse = (B2CConsumeCancelResponse) response.getResult();
        ConvertUtil.convertInput(b2CConsumeCancelResponse);
    }

}
